package com.hl.qzzpbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hl.qzzpbackend.common.DeleteRequest;
import com.hl.qzzpbackend.exception.BusinessException;
import com.hl.qzzpbackend.exception.ErrorCode;
import com.hl.qzzpbackend.exception.ThrowUtils;
import com.hl.qzzpbackend.model.dto.positionType.PositionTypeAddRequest;
import com.hl.qzzpbackend.model.dto.positionType.PositionTypeQueryRequest;
import com.hl.qzzpbackend.model.dto.positionType.PositionTypeUpdateRequest;
import com.hl.qzzpbackend.model.entity.IndustryType;
import com.hl.qzzpbackend.model.entity.PositionType;
import com.hl.qzzpbackend.model.vo.PositionTypeVO;
import com.hl.qzzpbackend.service.IndustryTypeService;
import com.hl.qzzpbackend.service.PositionTypeService;
import com.hl.qzzpbackend.mapper.PositionTypeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 岗位类型 业务层
 *
 * @author helong
*/
@Service
public class PositionTypeServiceImpl extends ServiceImpl<PositionTypeMapper, PositionType>
    implements PositionTypeService{

    @Resource
    private IndustryTypeService industryTypeService;

    @Resource
    private PositionTypeMapper positionTypeMapper;


    /**
     * 添加岗位类型
     * @param positionTypeAddRequest 添加岗位类型请求类
     * @return 是否成功
     */
    @Override
    public boolean positionTypeAdd(PositionTypeAddRequest positionTypeAddRequest) {
        Long industryTypeId = positionTypeAddRequest.getIndustryTypeId();
        String name = positionTypeAddRequest.getName();

        long count1 = industryTypeService.lambdaQuery().eq(IndustryType::getId,industryTypeId).count();
        ThrowUtils.throwIf(count1 == 0, ErrorCode.PARAMS_ERROR,"行业类型不存在");
        long count2 = lambdaQuery().eq(PositionType::getName,name).count();
        ThrowUtils.throwIf(count2 > 0, ErrorCode.PARAMS_ERROR,"岗位类型已存在");

        PositionType positionType = new PositionType();
        BeanUtil.copyProperties(positionTypeAddRequest,positionType);

        boolean result = this.save(positionType);
        if(!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"添加岗位类型失败");
        }
        return true;
    }

    /**
     * 修改岗位类型
     * @param positionTypeUpdateRequest 修改岗位类型请求类
     * @return 是否成功
     */
    @Override
    public boolean positionTypeUpdate(PositionTypeUpdateRequest positionTypeUpdateRequest) {
        Long id = positionTypeUpdateRequest.getId();
        Long industryTypeId = positionTypeUpdateRequest.getIndustryTypeId();
        String name = positionTypeUpdateRequest.getName();

        long count1 = lambdaQuery().eq(PositionType::getId,id).count();
        ThrowUtils.throwIf(count1 == 0, ErrorCode.PARAMS_ERROR,"岗位类型不存在");

        long count2 = lambdaQuery().eq(PositionType::getName,name).count();
        ThrowUtils.throwIf(count2 > 0, ErrorCode.PARAMS_ERROR,"岗位类型已存在");

        long count3 = industryTypeService.lambdaQuery().eq(IndustryType::getId,industryTypeId).count();
        ThrowUtils.throwIf(count3 == 0, ErrorCode.PARAMS_ERROR,"行业类型不存在");

        PositionType positionType = new PositionType();
        BeanUtil.copyProperties(positionTypeUpdateRequest,positionType);
        boolean result = this.updateById(positionType);
        if(!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"修改岗位类型失败");
        }
        return true;
    }

    /**
     * 删除岗位类型
     * @param deleteRequest 删除请求类
     * @return 是否成功
     */
    @Override
    public boolean positionTypeDelete(DeleteRequest deleteRequest) {
        Long id = deleteRequest.getId();

        long count1 = lambdaQuery().eq(PositionType::getId,id).count();
        ThrowUtils.throwIf(count1 == 0, ErrorCode.PARAMS_ERROR,"岗位类型不存在");

        boolean result = this.removeById(id);
        if(!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"删除岗位类型失败");
        }
        return true;
    }

    /**
     * 分页查询岗位类型
     * @param positionTypeQueryRequest 查询请求类
     * @return 岗位类型分页
     */
    @Override
    public Page<PositionTypeVO> getPositionTypePage(PositionTypeQueryRequest positionTypeQueryRequest) {
        int current = positionTypeQueryRequest.getCurrent();
        int pageSize = positionTypeQueryRequest.getPageSize();

        Page<PositionTypeVO> page = new Page<>(current,pageSize);
        return positionTypeMapper.getPage(page,positionTypeQueryRequest);
    }
}




